@extends('backstage.layout.master')
@section('title', '添加搜索')
@section('style')
@stop
@section('content')
    <div class="breadcrumbs" id="breadcrumbs">
        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a href="backstage/home">主页</a>
            </li>

            <li class="active">
                <a href="backstage/searchs/list">筛选列表</a>
            </li>
            <li>
                添加搜索
            </li>
        </ul><!-- .breadcrumb -->
    </div>

    <div class="page-content">
        <div class="row">
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                <div class="page-header">
                    <h1>
                        添加搜索
                    </h1>
                </div>
                <div class="row">
                    <div class="col-xs-12">
                        <!-- PAGE CONTENT BEGINS -->
                        <form id="myform" class="form-horizontal" role="form" action="backstage/searchs/add"
                              method="post" enctype="multipart/form-data">
                            <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

                            <div class="form-group product_form {{$errors->first('search_name')!=""?'has-error':''}}">
                                <label class="col-sm-4 control-label no-padding-right"> 搜索名称 </label>

                                <div class="col-sm-4">
                                    <input type="text" name="search_name" value="{{old('search_name')}}"
                                           id="search_name" placeholder="搜索名称"
                                           class="width-100">
                                </div>
                                <div class="col-sm-4">
                                    <div class="help-block col-xs-12 col-sm-reset inline">{{$errors->first('search_name')}} </div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="form-group product_form {{$errors->first('product_temp_categoty_id')!=""?'has-error':''}}">
                                <label class="col-sm-4 control-label no-padding-right"> 搜索分类(不同分类可搜索条件不同) </label>

                                <div class="col-sm-4">
                                    <select name="product_temp_categoty_id" id="product-temp-categoty">
                                        {{--<option value="0">全部</option>--}}
                                        @foreach($product_temp_categorys as $product_temp_category)
                                            <option value="{{$product_temp_category->product_temp_category_id}}">{{$product_temp_category->name}}</option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="col-sm-4">
                                    <div class="help-block col-xs-12 col-sm-reset inline">{{$errors->first('product_temp_categoty_id')}} </div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <input type="hidden" value="0" name="pid">
                            {{--<div class="form-group pid_form {{$errors->first('pid')!=""?'has-error':''}}">--}}
                                {{--<label class="col-sm-4 control-label no-padding-right"> 上级搜索 </label>--}}

                                {{--<div class="col-sm-4">--}}
                                    {{--<select name="pid" id="pid">--}}
                                        {{--<option data-type="0" value="0">一级搜索</option>--}}
                                        {{--@foreach($searchs as $search)--}}
                                            {{--<option data-type={{$search->type}} value="{{$search->search_id}}">{{$search->search_name}}--}}
                                                {{--（上级搜索）--}}
                                            {{--</option>--}}
                                        {{--@endforeach--}}
                                    {{--</select>--}}
                                {{--</div>--}}
                                {{--<div class="col-sm-4">--}}
                                    {{--<div class="help-block col-xs-12 col-sm-reset inline">{{$errors->first('pid')}} </div>--}}
                                {{--</div>--}}
                            {{--</div>--}}
                            {{--<div class="space-4"></div>--}}
                            <div class="form-group {{$errors->first('type')!=""?'has-error':''}}" id="type">
                                <label class="col-sm-4 control-label no-padding-right"> 类型(允许搜索的类型) </label>

                                <div class="col-sm-4">
                                    <select name="type" id="type-change">
                                        <option value="0">请选择搜索类型</option>
                                        <option value="1">价格区间搜索</option>
                                        <option value="2">适用人群搜索</option>
                                        <option value="3">产品品牌搜素</option>
                                        <option value="4">产品颜色搜索</option>
                                        <option value="5">产品款式搜索</option>
                                    </select>
                                </div>
                                <div class="col-sm-4">
                                    <div class="help-block col-xs-12 col-sm-reset inline">{{$errors->first('type')}}</div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="form-group" id="level" style="display:none;">
                                <label class="col-sm-4 control-label no-padding-right"> 价格分级(只支持单选) </label>

                                <div class="col-sm-4">
                                    <input type="text" value="" name="level" placeholder="价格分级">
                                </div>
                                <div class="col-sm-4">
                                    <div class="help-block col-xs-12 col-sm-reset inline">
                                        该选项表示该分类下商品最低价与最高价之间的划分的等级个数，填1表示不做分级，最多不能超过10个。价格由系统生成
                                    </div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="form-group" id="resource-id" style="display:none;">
                                <label class="col-sm-4 control-label no-padding-right resource-text"> 群体 </label>

                                <div class="col-sm-4">
                                    <select name="resource_id" id="resource-id-select">

                                    </select>
                                </div>
                                <div class="col-sm-4">
                                    <div class="help-block col-xs-12 col-sm-reset inline">{{$errors->first('resource_id')}}</div>
                                </div>
                            </div>
                            <div class="space-4"></div>

                            <div class="form-group" id="radio">
                                <label class="col-sm-4 control-label no-padding-right"> 选择方法 </label>

                                <div class="col-sm-4">
                                    <input type="radio" name="is_radio" value="0" id="hide"> <label
                                            for="hide">多选</label> <input type="radio" name="is_radio" checked
                                                                         value="1" id="show"> <label
                                            for="show">单选</label>
                                </div>
                                <div class="col-sm-4">
                                    <div class="help-block col-xs-12 col-sm-reset inline"></div>
                                </div>
                            </div>
                            <div class="space-4"></div>

                            <div class="col-sm-12" id="children-search">

                            </div>

                            <div id="more" class="form-group" align="center" style="display: none;">
                                <input class="btn" type="buttom" value="添加二级搜索" readonly onclick="addinput()"/>
                            </div>
                            <div class="space-4"></div>


                            <div class="clearfix form-actions">
                                <div class="col-md-offset-5 col-md-7">
                                    <button class="btn btn-info" type="submit">
                                        <i class="icon-ok bigger-110"></i>
                                        保存
                                    </button>

                                    &nbsp; &nbsp; &nbsp;
                                    <button class="btn" type="reset">
                                        <i class="icon-undo bigger-110"></i>
                                        重置
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div><!-- /.col -->
                </div>
                <!-- PAGE CONTENT ENDS -->
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.page-content -->
@stop
@section('script')
    <script src="assets/js/jquery-ui-1.10.3.custom.min.js"></script>
    <script src="assets/js/jquery.slimscroll.min.js"></script>
    <script type="text/javascript">
        var _token = '{{csrf_token()}}';
        var search_arr = '';
        $(function () {
            $("#product-temp-categoty").change(function () {
                var product_temp_category_id = $("#product-temp-categoty option:selected").val();
                $.post("backstage/searchs/search", {
                    product_temp_category_id: product_temp_category_id,
                    _token: _token
                }, function (data, status) {
                    var html = '<option data-type="0" value="0">一级分类</option>';
                    for (var a = 0; a < data.length; a++) {
                        html += '<option data-type="' + data[a].type + '" value="' + data[a].search_id + '">' + data[a].search_name + '（上级搜索）</option><br />';
                    }
                    $("#pid").empty();
                    $("#pid").append(html);
                    $(".pid_form").show();
                    $("#type-change").val("0");
                    $("#level").hide();
                });
            });
            $("#pid").change(function () {
                var type = $("#pid option:selected").attr('data-type');
                $("#children-search").empty();
                a = 0;
                search_arr = '';
                if (type == 0) {
                    $("#resource-id").hide();
                    if ($("#type-change option:selected").val() == 1) {
                        $("#level").show();
                    } else {
                        $("#level").hide();
                    }
                    $("#radio").show();
                    $("#type").show();
                } else {
                    $("#resource-id").show();
                    $("#level").hide();
                    $("#radio").hide();
                    $("#type").hide();
                    $("#more").hide();
                    take_metions(type);
                }
            });
            function take_metions(type) {
                switch (type) {
                    case '2':
                        searchCrown();
                        break;
                    case '3':
                        searchBrand();
                        break;
                    case '4':
                        searchColour();
                        break;
                    case '5':
                        searchStyle();
                        break;
                    default:
                        alert('请选择正确的类型');
                        return false;
                }
            }
            $("#type-change").change(function () {
                var type = $("#type-change option:selected").val();
                $("#children-search").empty();
                a = 0;
                search_arr = '';
                if (type == 1) {
                    $("#level").show();
                    $("#more").hide();
                } else {
                    $("#level").hide();
                    //获取
                    if(type != 0){
                        //出现添加二级分类
                        $("#more").show();
                        take_metions(type);
                    }
                }
            });

            function searchCrown() {
                $.ajax({
                    type: "POST",
                    url: "/backstage/searchs/search-crowd",
                    data: {'search': ''},
                    success: function (msg) {
                        console.log(msg);
                        if (msg.state == 1) {
                            var html = '';
                            for (var a = 0; a < msg['data'].length; a++) {
                                html += '<option value="' + msg['data'][a].id + '">' + msg['data'][a].crowd_name + '</option><br />';
                            }
                            search_arr = html;
                            $("#resource-id-select").empty();
                            $("#resource-id-select").append(html);
                            $(".resource-text").html('群体');
                        }
                    }, error: function () {
                        swal({title: "系统繁忙！", text: "", type: "error", timer: 1000})
                    }
                });
            }
        });
        function searchBrand() {
            var brand_keyword = '';
            $.ajax({
                type: "POST",
                url: "/backstage/searchs/search-brand",
                data: {'search': brand_keyword},
                success: function (msg) {
                    if (msg.state == 1) {
                        var html = '';
                        for (var a = 0; a < msg['data'].length; a++) {
                            html += '<option value="' + msg['data'][a].brand_id + '">' + msg['data'][a].brand_name + '</option><br />';
                        }
                        search_arr = html;
                        $("#resource-id-select").empty();
                        $("#resource-id-select").append(html);
                        $(".resource-text").html('品牌');
                    }
                }, error: function () {
                    swal({title: "系统繁忙！", text: "", type: "error", timer: 1000})
                }
            });
        }

        function searchColour() {
            keyword = '';
            $.ajax({
                type: "POST",
                url: "/backstage/searchs/search-colour",
                data: {'search': keyword},
                success: function (msg) {
                    if (msg.state == 1) {
                        var html = '';
                        for (var a = 0; a < msg['data'].length; a++) {
                            html += '<option value="' + msg['data'][a].id + '">' + msg['data'][a].colour_name + '</option><br />';
                        }
                        search_arr = html;
                        $("#resource-id-select").empty();
                        $("#resource-id-select").append(html);
                        $(".resource-text").html('颜色');
                    }
                }, error: function () {
                    swal({title: "系统繁忙！", text: "", type: "error", timer: 1000})
                }
            });
        }

        function searchStyle() {
            keyword = '';
            $.ajax({
                type: "POST",
                url: "/backstage/searchs/search-style",
                data: {'search': keyword},
                success: function (msg) {
                    console.log(msg);
                    if(msg.state == 1){
                        var html = '';
                        for (var a = 0; a < msg.result.length; a++) {
                            html += '<option value="' + msg.result[a].attr_name + '">' + msg.result[a].attr_name + '</option><br />';
                        }
                        search_arr = html;
                        $("#resource-id-select").empty();
                        $("#resource-id-select").append(html);
                        $(".resource-text").html('款式');
                    }
                }, error: function () {
                    swal({title: "系统繁忙！", text: "", type: "error", timer: 1000})
                }
            });
        }

        var a = 0;
        function addinput() {
            if(search_arr == ''){
                alert('正在加载必要参数，请稍后点击');
                return false;
            }
            console.log(search_arr);
            a++;
            var str = '<div class="form-group">' +
                    '<label class="col-sm-4 control-label no-padding-right resource-text"> 二级分类' + a + ' </label>' +

                    '<div class="col-sm-4">' +
                    '<input type="text" placeholder="搜索名称" name="child_search_name[]"> &nbsp;&nbsp;'+
                    '<select name="child_resource_id[]">' +
                    search_arr+
                    '</select>' +
                    '</div>' +
                    '<div class="col-sm-4">' +
                    '<div class="help-block col-xs-12 col-sm-reset inline"></div>' +
                    '</div>' +
                    '</div>' +
                    '<div class="space-4"></div>';
            $("#children-search").append(str);
        }

    </script>
@stop